﻿using System;
using System.Configuration;
using System.Data;
using mojoPortal.Net;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using ud_gSDMS.Code_Service;
using mojoPortal.Business.WebHelpers;
namespace ud_gSDMS.timesheet
{
    public partial class addnew : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private string _id;
        private SiteUser siteUser;
        private int currentUserID = 0;

        void Page_Load(object sender, EventArgs e)
        {
            //if (!UserCanEditPage() || !UserCanViewPage())
            //{
            //    WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            //}
            siteUser = SiteUtils.GetCurrentSiteUser();
            currentUserID = siteUser.UserId;
            if (
                //SiteUser.ConfirmRegistration()
                SiteUtils.GetCurrentSiteUser() == null
                )
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            }

            if (!Page.IsPostBack)
            {
                PopulateLabels();
                PopulateControls();
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    _id = Request["id"];
                    LoadData(_id);
                }
            }
        }
        private void PopulateLabels()
        {

        }
        private void PopulateControls()
        {
            //var minDay = Convert.ToDateTime(ConfigurationManager.AppSettings["MinDay"]);
            //var maxDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            datTimeSheet_Date.SelectedDate = DateTime.Now;
            var db = new gSDMS_DataLinq();
            var tbUser = Library.LINQToDataTable(db, db.mp_Users);

            DataTable tb = gSDMS_BL.getComboProjectCode(currentUserID);
            cboProject.DataSource = tb;
            cboProject.DataValueField = "Project_ID";
            cboProject.DataTextField = "Code";
            cboProject.DataBind();

            cboWork.DataValueField = "CAT_Project_Step_ID";
            cboWork.DataTextField = "Name";
            cboWork.DataSource = Library.LINQToDataTable(db, db.ud_gSDMS_CAT_PROJECT_STEPs);
            cboWork.DataBind();

            cboStatus.DataValueField = "CAT_TimeSheet_Status_ID";
            cboStatus.DataTextField = "Name";
            cboStatus.DataSource = Library.LINQToDataTable(db, db.ud_gSDMS_CAT_TIMESHEET_STATUS);
            cboStatus.DataBind();
            cboStatus.SelectedIndex = 0;

            if (gSDMS_BL.mp_User_IsRole(siteUser.UserId, "11"))
            {
                cboStatus.Enabled = true;
                cboStatus.SelectedValue = "2";
            }
            else
                cboStatus.Enabled = false;

            cboWorkStatus.SelectedIndex = 0;
            cboWorkStatus.Visible = false;
            //DataTable tb = Library.LINQToDataTable(db, db.ud_gSDMS_CAT_TIMESHEET_STATUS);
            //lbStatus.AccessKey = tb.Columns["CAT_TimeSheet_Status_ID"].ColumnName;
            //lbStatus.Text = tb.Columns["Name"].ColumnName;

            //lbStatus.DataBind();

            cboWorkStatus.DataValueField = "CAT_Work_Status_ID";
            cboWorkStatus.DataTextField = "Name";
            cboWorkStatus.DataSource = Library.LINQToDataTable(db, db.ud_gSDMS_CAT_WORK_STATUS);
            cboWorkStatus.DataBind();
            //RECEIVING & TRANFER
            //Set status
            const bool readOnly = true;
            //txtContract_Code.ReadOnly = readOnly;
        }
        private void LoadData(string id)
        {
            if (string.IsNullOrEmpty(id)) return;
            var o = gSDMS_BL.ud_gSDMS_TIMESHEET_Get(id);
            if (o != null)
            {
                hidId.Value = o.TimeSheet_ID.ToString();
                // if (o.CreatedOn!= null) hidCreatedOn.Value = o.CreatedOn.Value.ToShortDateString();
                if (o.Date != null) datTimeSheet_Date.SelectedDate = o.Date;
                cboProject.SelectedValue = o.Project_ID.ToString();
                txtFrom.Text = o.From.ToString();
                txtTo.Text = o.To.ToString();
                txtHours.Text = o.Hours.ToString();
                cboWork.SelectedValue = o.CAT_Project_Step_ID.ToString();
                cboStatus.SelectedValue = o.CAT_TimeSheet_Status_ID.ToString();
                txtComplete.Text = o.Percent_Complete.ToString();
                txtDescription.Text = o.Description;
                cboWorkStatus.SelectedValue = o.CAT_Work_Status_ID.ToString();
                txtFeedback.Text = o.FeedBack;
                if(txtFeedback.Text.Trim().Length>0)
                {
                    txtFeedback.Visible = true;
                    lblfeedback.Text = "FeedBack :";
                    lblfeedback.Visible = true;
                }
            }
        }
        private void LoadGrid()
        {
            if (ViewState["listMember"] != null)
            {
                var data = (DataTable)ViewState["listMember"];
                //grdMember.DataSource = data;
                //grdMember.DataBind();
            }
        }
        private bool CheckData()
        {
            var isValid = true;
            /*
            var t = txtDate.Text;
            if (t.Split('/').Length < 8 || t.Split('/')[4] == "")
            {
                isValid = false;
                req1.IsValid = false;
                txtDate.Focus();
                return isValid;
            }
            */
            return isValid;
        }
        private DataTable CreateTable()
        {
            var dataTable = new DataTable();
            DataColumn col;

            col = new DataColumn("Project_Role_ID");
            dataTable.Columns.Add(col);

            col = new DataColumn("UserID");
            dataTable.Columns.Add(col);

            col = new DataColumn("UserName");
            dataTable.Columns.Add(col);

            col = new DataColumn("Name");
            dataTable.Columns.Add(col);

            col = new DataColumn("CAT_User_Role_ID");
            dataTable.Columns.Add(col);
            col = new DataColumn("RoleName");
            dataTable.Columns.Add(col);
            return dataTable;
        }

        #region event
        void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                _id = hidId.Value;
                var userSite = SiteUtils.GetCurrentSiteUser();
                if (cboStatus.SelectedValue == "3")
                {
                    cboStatus.SelectedValue = "1";
                }
                gSDMS_BL.ud_gSDMS_TIMESHEET_Upd(ref _id, cboProject.SelectedValue, cboWork.SelectedValue, currentUserID.ToString(),
                    datTimeSheet_Date.SelectedDate != null ? datTimeSheet_Date.SelectedDate.Value.ToShortDateString() : null,
                    txtFrom.Text, txtTo.Text, txtHours.Text, cboStatus.SelectedValue,
                    cboWorkStatus.SelectedValue, txtComplete.Text, txtDescription.Text, txtFeedback.Text, null, null, currentUserID.ToString(), DateTime.Now.ToShortDateString());
                if (!string.IsNullOrEmpty(hidId.Value))
                {
                    lblMes.Text = @"Updated Success....!";
                    lblMes.Visible = true;
                    Response.Redirect("/gSDMSModule/timesheet/list.aspx");
                }
                else
                {
                    lblMes.Text = @"Insert Sucess....!";
                    lblMes.Visible = true;
                    Response.Redirect("/gSDMSModule/timesheet/list.aspx");
                }
            }
            catch (Exception ex)
            {
                lblMes.Text = ex.Message;
                lblMes.Visible = true;
            }
        }

        void datTimeSheet_Date_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            if (datTimeSheet_Date.SelectedDate != null)
            {
                datTimeSheet_Date.MinDate = (DateTime)datTimeSheet_Date.SelectedDate;
            }
        }

        #endregion event

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            btnSave.Click += new EventHandler(btnSave_Click);
            datTimeSheet_Date.SelectedDateChanged += datTimeSheet_Date_SelectedDateChanged;
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);

            //btnSave.Click+=btnSave_Click;
            //datContract_Date.SelectedDateChanged += datContract_Date_SelectedDateChanged;
            //datStart_Date.SelectedDateChanged += datStart_Date_SelectedDateChanged;
            //datEnd_Date.SelectedDateChanged += datEnd_Date_SelectedDateChanged;
            //btnAdd.Click += btnAdd_Click;

            //grdMember.RowCommand += grdMember_RowCommand;
            //grdMember.RowDeleting += grdMember_RowDeleting;
            //btnFinish.Click += btnFinish_Click;
            SuppressPageMenu();
        }
        //void btnFinish_Click(object sender, EventArgs e)
        //{
        //    WebUtils.SetupRedirect(this, SiteUtils.GetCurrentPageUrl());
        //}

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            //config = new ContactFormConfiguration(settings);
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion

        protected void txtContract_Code_TextChanged(object sender, EventArgs e)
        {

        }
    }
}